﻿// Test_RtpProtocol.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <tchar.h>
#include <time.h>
#include <windows.h>
#include <list>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_RtpProtocol/RTPProtocol_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_StreamMedia/StreamMedia_RtpProtocol/RTPProtocol_Error.h"

#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/StreamMedia_RtpProtocol.lib")
#pragma comment(lib,"Ws2_32.lib")


void TestPacket_RTP264()
{
	XNETHANDLE xhSsrc = 0;
	if (!RTPProtocol_Packet_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H264, FALSE))
	{
		printf("errrno");
		return;
	}
	RTPProtocol_Packet_SetInfo(xhSsrc);

	LPCTSTR lpsz264File = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\1080P.264");
	LPCTSTR lpszRTPFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\1080P.RTP");

	FILE *pSt_264File = _tfopen(lpsz264File, _T("rb"));
	FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("wb"));
	TCHAR tszMsgBuffer[10240];
	int i = 0;

	while (1)
	{
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_264File);
		if (nRet <= 0)
		{
			break;
		}
		RTPProtocol_Packet_Send(xhSsrc, tszMsgBuffer, nRet);
		while (1)
		{
			list<STREAMMEDIA_RTPPROTOCOL_PACKET> stl_RTPPacket;
			if (!RTPProtocol_Packet_Recv(xhSsrc, &stl_RTPPacket))
			{
				break;
			}
			list<STREAMMEDIA_RTPPROTOCOL_PACKET>::const_iterator stl_ListIterator = stl_RTPPacket.begin();
			for (; stl_ListIterator != stl_RTPPacket.end(); stl_ListIterator++)
			{
				printf("%d=%d\n", i, stl_ListIterator->nMsgLen);
				fwrite(stl_ListIterator->tszMsgBuffer, 1, stl_ListIterator->nMsgLen, pSt_RTPFile);
				i++;
			}
			stl_RTPPacket.clear();
		}
	}
	fclose(pSt_264File);
	fclose(pSt_RTPFile);
	RTPProtocol_Packet_Destory(xhSsrc);
}
void TestParse_RTP264()
{
	XNETHANDLE xhSsrc = 0;
	if (!RTPProtocol_Parse_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H264, FALSE))
	{
		printf("errrno");
		return;
	}
	LPCTSTR lpsz264File = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\1080_1.264");
	LPCTSTR lpszRTPFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\1080P.RTP");

	FILE* pSt_264File = _tfopen(lpsz264File, _T("wb"));
	FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("rb"));
	TCHAR tszMsgBuffer[10240];
	int i = 0;

	while (1)
	{
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_RTPFile);
		if (nRet <= 0)
		{
			break;
		}
		RTPProtocol_Parse_Send(xhSsrc, tszMsgBuffer, nRet);
		while (1)
		{
			int nMsgLen = 1024000;
			TCHAR* ptszMsgBuffer = new TCHAR[nMsgLen];
			STREAMMEDIA_RTPPROTOCOL_HDR st_RTPHdr;

			memset(ptszMsgBuffer, '\0', nMsgLen);
			memset(&st_RTPHdr, '\0', sizeof(STREAMMEDIA_RTPPROTOCOL_HDR));

			if (!RTPProtocol_Parse_Recv(xhSsrc, ptszMsgBuffer, &nMsgLen, &st_RTPHdr, NULL, NULL))
			{
				delete[] ptszMsgBuffer;
				ptszMsgBuffer = NULL;
				break;
			}
			printf("%d\n", i++);
			fwrite(ptszMsgBuffer, 1, nMsgLen, pSt_264File);
			delete[] ptszMsgBuffer;
			ptszMsgBuffer = NULL;
		}
	}
	fclose(pSt_264File);
	fclose(pSt_RTPFile);
	RTPProtocol_Parse_Destory(xhSsrc);
}
void TestPacket_RTP265()
{
	XNETHANDLE xhSsrc = 0;
	if (!RTPProtocol_Packet_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H265,FALSE))
	{
		printf("errrno");
		return;
	}

	RTPProtocol_Packet_SetInfo(xhSsrc);
	LPCTSTR lpsz264File = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\2.hevc");
	LPCTSTR lpszRTPFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\hevc.RTP");

	FILE* pSt_264File = _tfopen(lpsz264File, _T("rb"));
	FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("wb"));
	TCHAR tszMsgBuffer[10240];
	int i = 0;

	while (1)
	{
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_264File);
		if (nRet <= 0)
		{
			break;
		}
		RTPProtocol_Packet_Send(xhSsrc, tszMsgBuffer, nRet);
		while (1)
		{
			list<STREAMMEDIA_RTPPROTOCOL_PACKET> stl_RTPPacket;
			if (!RTPProtocol_Packet_Recv(xhSsrc, &stl_RTPPacket))
			{
				break;
			}
			list<STREAMMEDIA_RTPPROTOCOL_PACKET>::const_iterator stl_ListIterator = stl_RTPPacket.begin();
			for (; stl_ListIterator != stl_RTPPacket.end(); stl_ListIterator++)
			{
				printf("%d=%d\n", i, stl_ListIterator->nMsgLen);
				fwrite(stl_ListIterator->tszMsgBuffer, 1, stl_ListIterator->nMsgLen, pSt_RTPFile);
				i++;
			}
			stl_RTPPacket.clear();
		}
	}
	fclose(pSt_264File);
	fclose(pSt_RTPFile);
	RTPProtocol_Packet_Destory(xhSsrc);
}
void TestParse_RTP265()
{
	XNETHANDLE xhSsrc = 0;
	if (!RTPProtocol_Parse_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_H265, FALSE))
	{
		printf("errrno");
		return;
	}
	LPCTSTR lpsz264File = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\2_1.hevc");
	LPCTSTR lpszRTPFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\hevc.RTP");

	FILE* pSt_264File = _tfopen(lpsz264File, _T("wb"));
	FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("rb"));
	TCHAR tszMsgBuffer[10240];
	int i = 0;

	while (1)
	{
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_RTPFile);
		if (nRet <= 0)
		{
			break;
		}
		RTPProtocol_Parse_Send(xhSsrc, tszMsgBuffer, nRet);
		while (1)
		{
			int nMsgLen = 1024000;
			TCHAR* ptszMsgBuffer = new TCHAR[nMsgLen];
			STREAMMEDIA_RTPPROTOCOL_HDR st_RTPHdr;

			memset(ptszMsgBuffer, '\0', nMsgLen);
			memset(&st_RTPHdr, '\0', sizeof(STREAMMEDIA_RTPPROTOCOL_HDR));

			if (!RTPProtocol_Parse_Recv(xhSsrc, ptszMsgBuffer, &nMsgLen, &st_RTPHdr, NULL, NULL))
			{
				delete[] ptszMsgBuffer;
				ptszMsgBuffer = NULL;
				break;
			}
			printf("%d=%d\n", i++, nMsgLen);
			fwrite(ptszMsgBuffer, 1, nMsgLen, pSt_264File);
			delete[] ptszMsgBuffer;
			ptszMsgBuffer = NULL;
		}
	}
	fclose(pSt_264File);
	fclose(pSt_RTPFile);
	RTPProtocol_Parse_Destory(xhSsrc);
}
void TestPacket_RTPAAC()
{
	XNETHANDLE xhSsrc = 0;
	if (!RTPProtocol_Packet_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_AAC, FALSE))
	{
		printf("errrno");
		return;
	}
	LPCTSTR lpszAACFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\test.aac");
	LPCTSTR lpszRTPFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\test.RTP");

	FILE* pSt_264File = _tfopen(lpszAACFile, _T("rb"));
	FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("wb"));
	TCHAR tszMsgBuffer[10240];
	int i = 0;

	while (1)
	{
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_264File);
		if (nRet <= 0)
		{
			break;
		}
		RTPProtocol_Packet_Send(xhSsrc, tszMsgBuffer, nRet);
		while (1)
		{
			list<STREAMMEDIA_RTPPROTOCOL_PACKET> stl_RTPPacket;
			if (!RTPProtocol_Packet_Recv(xhSsrc, &stl_RTPPacket))
			{
				break;
			}
			list<STREAMMEDIA_RTPPROTOCOL_PACKET>::const_iterator stl_ListIterator = stl_RTPPacket.begin();
			for (; stl_ListIterator != stl_RTPPacket.end(); stl_ListIterator++)
			{
				printf("%d=%d\n", i, stl_ListIterator->nMsgLen);
				fwrite(stl_ListIterator->tszMsgBuffer, 1, stl_ListIterator->nMsgLen, pSt_RTPFile);
				i++;
			}
			stl_RTPPacket.clear();
		}
	}
	fclose(pSt_264File);
	fclose(pSt_RTPFile);
	RTPProtocol_Packet_Destory(xhSsrc);
}
void TestParse_RTPAAC()
{
	XNETHANDLE xhSsrc = 0;
	if (!RTPProtocol_Parse_Init(&xhSsrc, ENUM_STREAMMEDIA_RTPPROTOCOL_PAYLOAD_TYPE_AAC, FALSE))
	{
		printf("errrno");
		return;
	}
	RTPProtocol_Packet_SetInfo(xhSsrc, 1024, 44100);
	LPCTSTR lpsz264File = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\test_1.aac");
	LPCTSTR lpszRTPFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\test.RTP");

	FILE* pSt_264File = _tfopen(lpsz264File, _T("wb"));
	FILE* pSt_RTPFile = _tfopen(lpszRTPFile, _T("rb"));
	TCHAR tszMsgBuffer[10240];
	int i = 0;

	while (1)
	{
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		int nRet = fread(tszMsgBuffer, 1, sizeof(tszMsgBuffer), pSt_RTPFile);
		if (nRet <= 0)
		{
			break;
		}
		RTPProtocol_Parse_Send(xhSsrc, tszMsgBuffer, nRet);
		while (1)
		{
			int nMsgLen = 1024000;
			TCHAR* ptszMsgBuffer = new TCHAR[nMsgLen];
			STREAMMEDIA_RTPPROTOCOL_HDR st_RTPHdr;

			memset(ptszMsgBuffer, '\0', nMsgLen);
			memset(&st_RTPHdr, '\0', sizeof(STREAMMEDIA_RTPPROTOCOL_HDR));

			if (!RTPProtocol_Parse_Recv(xhSsrc, ptszMsgBuffer, &nMsgLen, &st_RTPHdr, NULL, NULL))
			{
				delete[] ptszMsgBuffer;
				ptszMsgBuffer = NULL;
				break;
			}
			printf("%d\n", i++);
			fwrite(ptszMsgBuffer, 1, nMsgLen, pSt_264File);
			delete[] ptszMsgBuffer;
			ptszMsgBuffer = NULL;
		}
	}
	fclose(pSt_264File);
	fclose(pSt_RTPFile);
	RTPProtocol_Parse_Destory(xhSsrc);
}
int main()
{
	TestPacket_RTP265();
	TestParse_RTP265();

	TestPacket_RTPProtocol();
	TestParse_RTPProtocol();

	TestPacket_RTPAAC();
	TestParse_RTPAAC();
	return 0;
}